package com.googlecode.connectlet.tsa;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import sun.security.timestamp.HttpTimestamper;
import sun.security.timestamp.TSRequest;
import sun.security.timestamp.TSResponse;

import com.googlecode.connectlet.ssl.CertMap;
import com.googlecode.connectlet.util.ClassPath;

public class TestTsaHttp {
	public static void main(String[] args) throws Exception {
		Server server = new Server();
		SocketConnector connector = new SocketConnector();
		connector.setPort(80);
		server.addConnector(connector);
		ServletHandler handler = new ServletHandler();
		ServletHolder holder = new ServletHolder(new TsaServlet());
		holder.setInitParameter("keystoreFile",
				ClassPath.getInstance("../conf/localhost.pfx").getPath());
		holder.setInitParameter("keystorePass", "changeit");
		holder.setInitParameter("keystoreType", "PKCS12");
		handler.addServletWithMapping(holder, "/");
		server.setHandler(handler);
		server.start();

		Thread.sleep(1000);
		TSRequest req = new TSRequest(new byte[16], "MD5");
		req.requestCertificate(true);
		TSResponse resp = new HttpTimestamper("http://localhost/").generateTimestamp(req);

		System.out.println(resp.getToken());
		CertMap certMap = new CertMap();
		certMap.add(ClassPath.getInstance("../conf/localhost.cer"));
		System.out.println(P7S.verify(resp.getToken(), certMap));
		System.out.println(new TstInfo(resp.getToken()).getTimeStamp());

		server.stop();
	}
}